<template>
    <div>
        <!--卡片-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>营销管理</el-breadcrumb-item>
                <el-breadcrumb-item>客服问答</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="questionFormData"  class="demo-form-inline">
                <el-form-item >
                    <el-input v-model="questionFormData.submodule" placeholder="请输入所属模块"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="initQuestionTable">查询</el-button>
                    <el-button type="primary" @click="addQuestion">添加</el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <template>
                <el-table
                        :data="tableData"
                        :border="true"
                        stripe
                        style="width: 100%">
                    <el-table-column
                            prop="id"
                            label="ID"
                            width="100">
                    </el-table-column>
                    <el-table-column
                            prop="submodule"
                            label="所属模块"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="question"
                            label="问题">
                    </el-table-column>
                    <el-table-column
                            prop="answer"
                            label="答案">
                    </el-table-column>
                    <el-table-column
                            prop="addtime"
                            label="添加时间">
                    </el-table-column>

                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" circle @click="changeTable(scope.row.id)">编辑</el-button>
                            <el-button type="danger" icon="el-icon-delete" circle @click="del(scope.row.id)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <!--                分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
                <!--新增弹窗-->
                <el-dialog
                        title="提示"
                        :visible.sync="addDialogVisible"
                        width="30%"
                >
                    <!--model:表单数据-->
                    <el-form  label-width="80px" :model="addForm" :rules="rules" ref="addForm">
                        <el-form-item label="所属模块" prop="submodule">
                            <el-input v-model="addForm.submodule"></el-input>
                        </el-form-item>
                        <el-form-item label="问题" prop="question">
                            <el-input v-model="addForm.question"></el-input>
                        </el-form-item>
                        <el-form-item label="答案" prop="answer">
                            <el-input v-model="addForm.answer"></el-input>
                        </el-form-item>

                    </el-form>
                    <span slot="footer" class="dialog-footer">
                        <el-button @click="addDialogVisible = false">取 消</el-button>
                        <el-button type="primary" @click="addbyQuestionId('addForm')">确 定</el-button>
                    </span>
                </el-dialog>
                <!--修改弹窗-->
                <el-dialog
                        title="提示"
                        :visible.sync="changeDialogVisible"
                        width="30%"
                >
                    <!--model:表单数据-->
                    <el-form  label-width="80px" :model="changeForm" :rules="rules" ref="changeForm">
                        <el-form-item label="所属模块" prop="submodule">
                            <el-input v-model="changeForm.submodule"></el-input>
                        </el-form-item>
                        <el-form-item label="问题" prop="question">
                            <el-input v-model="changeForm.question"></el-input>
                        </el-form-item>
                        <el-form-item label="答案" prop="answer">
                            <el-input v-model="changeForm.answer"></el-input>
                        </el-form-item>
                    </el-form>
                    <span slot="footer" class="dialog-footer">
                        <el-button @click="changeDialogVisible = false">取 消</el-button>
                        <el-button type="primary" @click="changebyQuestionId(`changeForm`)">确 定</el-button>
                    </span>
                </el-dialog>
                <!--删除确认弹窗-->
                <el-dialog
                        title="提示"
                        :visible.sync="dialogVisible"
                        width="30%"
               >
                <span>是否确认删除</span>
                <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="delQuestion()">确 定</el-button>
                </span>
                </el-dialog>

            </template>
        </el-card>

    </div>
</template>

<script>
    export default {
        name: "Question",
        data(){
            return {
                //条件搜索对象
                questionFormData:{},
                //表格数据
                tableData:[],
                //每页显示的条数
                pageSize: 5,
                //当前的显示的页码
                currentPage:1,
                //总条数
                total:0,
                addForm:{
                    submodule:'',
                    question:'',
                    answer:'',
                },
                changeForm:[],
                //新增弹窗
                addDialogVisible:false,

                //声明当前的角色id号
                delid: 0,
                //删除确认弹窗
                dialogVisible:false,
                //修改弹窗
                changeDialogVisible:false,
                rules:{
                    submodule: [
                        { required: true, message: '请输入所属模块', trigger: 'blur' },
                        { min: 2, max: 9, message: '长度在 2 到 9 个字符', trigger: 'blur' }
                    ],
                    question: [
                        { required: true, message: '请输入问题', trigger: 'blur' }
                    ],
                    answer: [
                        { required: true, message: '请输入答案', trigger: 'blur' }
                    ],
                    addtime: [
                        { required: true, message: '请输入日期', trigger: 'blur' }
                    ]
                },
            }
        },
        created(){
            this.initQuestionTable();
        },
        methods:{
            //初始化客服表
            initQuestionTable(){
                var that= this;
                this.$http.post(`/market/question/initQuestionTable/${this.currentPage}/${this.pageSize}`,this.questionFormData).then(function (r) {
                    that.tableData=r.data.result.records;
                    that.total=r.data.result.total;
                })
            },
            //新增
            addQuestion(){
                var that=this;
                that.addDialogVisible=true;
                that.addForm={};
            },
            //确认新增
            addbyQuestionId(formName){
                var that=this;
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$http.post(`/market/question/addbyQuestionId`,this.addForm).then(function (resp) {
                                that.$message({
                                    message: '添加成功！！！',
                                    type: 'success'
                                });
                            that.addDialogVisible=false;
                            that.initQuestionTable();
                        });
                    } else {
                        return false;
                    }
                });
            },

            //删除
            del(id){
                var that=this;
                that.delid=id;
                that.dialogVisible=true;
            },
            //确认删除
            delQuestion(){
                var that=this;
                this.$http.delete("/market/question/delQuestion?id="+this.delid).then(function (resp) {
                    that.dialogVisible=false;
                    that.initQuestionTable();
                    that.$message({
                        message: '删除成功！！！',
                        type: 'success'
                    });
                })
            },

            //编辑
            changeTable(id){
                var that=this;
                that.changeDialogVisible=true;
                this.$http.get("/market/question/chaXun?id="+id).then(function (resp) {
                    that.changeForm=resp.data.result.questions[0];
                })
            },
            //确认编辑
            changebyQuestionId(formName){
                var that=this;
                this.$refs[formName].validate((valid) => {
                    if (valid){
                        this.$http.post(`/market/question/changeQuestion`,this.changeForm).then(function (resp) {
                            that.changeDialogVisible=false;
                            that.initQuestionTable();
                            that.$message({
                                message: '修改成功！！！',
                                type: 'success'
                            });
                        })
                    } else {
                        return false;
                    }
                });

            },

            //当改变没有显示的条数时触发该方法
            handleSizeChange(y){
                this.pageSize=y;
                this.initQuestionTable();
            },
            //切换页码时触发该方法
            handleCurrentChange(y){
                this.currentPage=y;
                this.initQuestionTable();
            }

        }
    }
</script>

<style scoped>

</style>